#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QTime>
#include <QDebug>
#include "vector"
#include <iostream>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <time.h>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    QSerialPort serialPort;
    bool isOpen=false;

    QFile dataFile;
    QTextStream out;

    //通过QDatastream组装成数据帧
#pragma pack(push,1)
    typedef struct
    {
        quint16 head;               //索引 0xA55A
        quint8 status;             //工作状态
        float pitch=0;                //俯仰角（°）
        float roll=0;                  //横滚角
        float yaw=0;                  //方位角
        float latitude=0;             //当前纬度
        float Wx=0;                   //X轴角速率（°/s）
        float Wy=0;                   //Y轴角速率
        float Wz=0;                   //Z轴角速率
        float Ax=0;                   //X轴加速度（°/s²）
        float Ay=0;                   //Y轴加速度
        float Az=0;                   //Z轴加速度
        quint8 temperature=0;           //摄氏度
        quint8 id=0;                    //发送序号0~255循环递增
        quint8 check=0;                 //校验字节，第2-44字节异或
        quint8 tail;             //帧尾0x55
    }RECEIVE_FROM_INS;
    #pragma pack(pop)
//    RECEIVE_FROM_INS deserializeFrame(QByteArray& data)
//    {
//        int size=sizeof(RECEIVE_FROM_INS);
//        qDebug()<<"size:"<<size;
//        RECEIVE_FROM_INS frame;
//        memcpy(&frame,data.constData(),size*2);

//    }


//    double check(SEND_TO_PLC);

//    QByteArray serializeFrame(SEND_TO_PLC);

    //mainwindow.cpp文件中无法使用
    RECEIVE_FROM_INS deserializeFrame(QByteArray& data)
    {
        /*⚠注意：串口得到的data实际上是字符的ascii码数组⚠*/
        RECEIVE_FROM_INS frame;
        QByteArray rawData=QByteArray::fromHex(data);
        QDataStream stream(rawData);
//        qDebug()<<"串口数据1："<<data.left(47*2);
//        qDebug()<<"data"<<data;
        stream.setByteOrder(QDataStream::LittleEndian);
        stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
//        stream.readRawData(frame.head,4);
//        stream.readRawData(frame.status,2);
        quint16 head;
        quint8 status,temperature,id,check,tail;
        float pitch,roll,yaw,latitude,Wx,Wy,Wz,Ax,Ay,Az;
        stream>>head>>status>>pitch>>roll>>yaw>>latitude>>Wx>>Wy>>Wz>>Ax>>Ay>>Az>>temperature>>id>>check>>tail;

        frame.head=head;
        frame.status=status;
        frame.pitch=pitch;
        frame.roll=roll;
        frame.yaw=yaw;
        frame.latitude=latitude;
        frame.Wx=Wx;
        frame.Wy=Wy;
        frame.Wz=Wz;
        frame.Ax=Ax;
        frame.Ay=Ay;
        frame.Az=Az;
        frame.temperature=temperature;
        frame.id=id;
        frame.check=check;
        frame.tail=tail;
        qDebug()<<"pitch:"<<frame.pitch<<"roll:"<<frame.roll<<"yaw:"<<frame.yaw<<"Wx:"<<frame.Wx<<"Wy:"<<frame.Wy<<"Wz:"<<frame.Wz<<"Ax:"<<frame.Ax<<"Ay:"<<frame.Ay<<"Az:"<<frame.Az;
        saveFrameToCSV(frame,out);
        return frame;
    }

    void saveFrameToCSV(const RECEIVE_FROM_INS& frame,QTextStream& out)
    {
        out <<QString::number(frame.head)<<","
            <<QString::number(frame.status)<<","
            <<frame.pitch<<","<<frame.roll<<","<<frame.yaw<<","<<frame.latitude<<","
            <<frame.Wx<<","<<frame.Wy<<","<<frame.Wz<<","
            <<frame.Ax<<","<<frame.Ay<<","<<frame.Az<<","
            <<QString::number(frame.temperature)<<","<<QString::number(frame.id)<<","
            <<QString::number(frame.check)<<","<<QString::number(frame.tail)<<"\n";
    }

private slots:

    void readMsg();


//    void writeReal(QDataStream& out, float f);

    void on_com_connect_clicked();

    void on_com_disconnect_clicked();

private:
    Ui::MainWindow *ui;

};
#endif // MAINWINDOW_H
